package com.chinatelecom.clientservice.service.impl;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chinatelecom.clientservice.dao.dev.BaseFieldsMapper;
import com.chinatelecom.clientservice.dao.dev.BaseMenuMapper;
import com.chinatelecom.clientservice.dao.dev.BaseUsersMapper;
import com.chinatelecom.clientservice.pojo.BaseFields;
import com.chinatelecom.clientservice.pojo.BaseFieldsExample;
import com.chinatelecom.clientservice.pojo.BaseMenu;
import com.chinatelecom.clientservice.pojo.BaseMenuExample;
import com.chinatelecom.clientservice.pojo.BaseUsers;
import com.chinatelecom.clientservice.pojo.BaseUsersExample;
import com.chinatelecom.clientservice.pojo.BaseUsersExample.Criteria;
import com.chinatelecom.clientservice.pojo.Tree;
import com.chinatelecom.clientservice.pojo.TreeMenu;
import com.chinatelecom.clientservice.service.SystemService;
import com.google.gson.Gson;

@Service
public class SystemServiceImpl implements SystemService {
	@Autowired
	private BaseUsersMapper baseUsersMapper;
	@Autowired
	private BaseMenuMapper baseMenuMapper;
	@Autowired
	private BaseFieldsMapper baseFieldsMapper;

	public BaseUsers selectByBaseUser(Map<String, String> params) {
		BaseUsersExample example = new BaseUsersExample();
		Criteria criteria = example.createCriteria();
		criteria.andAccountEqualTo(params.get("account"));
		criteria.andPasswordEqualTo(params.get("password"));
		List<BaseUsers> results = baseUsersMapper.selectByExample(example);

		if (results == null || results.isEmpty()) {
			return null;
		}

		return results.get(0);
	}

	public Tree selectMenuByUser(BaseUsers baseUser) {
		BaseMenuExample example = new BaseMenuExample();
		com.chinatelecom.clientservice.pojo.BaseMenuExample.Criteria criteria = example.createCriteria();
		criteria.andIsDisplayEqualTo(1);
		example.setOrderByClause("display_index asc");
		List<BaseMenu> menuList = baseMenuMapper.selectByExample(example);
		TreeMenu treeMenu = new TreeMenu(menuList);
		return treeMenu.getTreeJson();
	}

	public Map<String, String> selectSystemParams() {
		BaseFieldsExample example = new BaseFieldsExample();
		com.chinatelecom.clientservice.pojo.BaseFieldsExample.Criteria criteria = example.createCriteria();
		criteria.andEnabledEqualTo(1);
		example.setOrderByClause(" field desc ,sort asc ");
		List<BaseFields> baseFieldList = baseFieldsMapper.selectByExample(example);

		HashMap<String, LinkedHashMap<String, String>> all = new HashMap<String, LinkedHashMap<String, String>>();
		LinkedHashMap<String, String> part = null;
		for (int i = 0; i < baseFieldList.size(); i++) {
			BaseFields baseFields = baseFieldList.get(i);
			String key = baseFields.getField();
			if (all.containsKey(key)) {
				// 如果包含这个field，就加入它的值
				part = all.get(key);
				part.put(baseFields.getValueField(), baseFields.getDisplayField());
			} else {
				part = new LinkedHashMap<String, String>();
				part.put(baseFields.getValueField(), baseFields.getDisplayField());
				// 没有这个fiel，则新加入这个filed
				all.put(key, part);
			}
		}
		part = new LinkedHashMap<String, String>();
		Gson gson = new Gson();
		for (Entry<String, LinkedHashMap<String, String>> entry : all.entrySet()) {
			String key = entry.getKey();
			HashMap<String, String> value = entry.getValue();
			// 为了eval('(${applicationScope.fields.sex})')这个单引号使用,替换所有的'，为\'
			String val = gson.toJson(value).replaceAll("\\'", "\\\\'");
			part.put(key, val);
		}
		return part;
	}

}
